package com.demo.rabbitmq.topic;

import com.demo.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.nio.charset.StandardCharsets;

public class Producer {

    public static void main(String[] args) throws Exception{

        Connection connection = ConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        String exchangeName = "test_topic";

        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC, true, false, false, null);

        String queue1Name = "test_topic_queue1";
        String queue2Name = "test_topic_queue2";

        channel.queueDeclare(queue1Name, true, false, false, null);
        channel.queueDeclare(queue2Name, true, false, false, null);

        //绑定队列和交换机
        channel.queueBind(queue1Name, exchangeName, "#.error");
        channel.queueBind(queue1Name, exchangeName, "order.*");
        channel.queueBind(queue2Name, exchangeName, "*.*");

        //String message= "[所在系统：order][日志级别：info][日志内容：订单生成，保持成功]";
        //channel.basicPublish(exchangeName, "order.info", null, message.getBytes(StandardCharsets.UTF_8));

        //String message= "[所在系统：goods][日志级别：info][日志内容：商品发布成功]";
        //channel.basicPublish(exchangeName, "goods.info", null, message.getBytes(StandardCharsets.UTF_8));

        String message= "[所在系统：goods][日志级别：error][日志内容：商品发布失败]";
        channel.basicPublish(exchangeName, "goods.error", null, message.getBytes(StandardCharsets.UTF_8));

        channel.close();
        connection.close();
    }
}
